package com.kungfuhacking.wristbandpro.find.presenter;

import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.find.activity.SubInfoActivity;
import com.kungfuhacking.wristbandpro.find.bean.ConfirmBean;
import com.kungfuhacking.wristbandpro.find.bean.ConfirmRecordBean;
import com.kungfuhacking.wristbandpro.find.view.IDConfirmeView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;

import io.reactivex.disposables.Disposable;

/**
 * Created by Aaron on 17/7/14.
 */

public class IIDConfirmePresenter implements BasePresenter {

    private IDConfirmeView idConfirmeView;

    public IIDConfirmePresenter(IDConfirmeView idConfirmeView) {
        this.idConfirmeView = idConfirmeView;
    }

    private String TAG = getClass().getSimpleName();

    /**
     * 获取认证记录
     */
    public void getIdentityList() {

        JSONObject j = new JSONObject();
        j.put("token", idConfirmeView.getToken());

        RetrofitHttpUtil.getInstance().identityList(new BaseNoTObserver<ConfirmRecordBean>() {
            @Override
            public void onHandleSuccess(ConfirmRecordBean basebean) {
                idConfirmeView.hideLoading();

                idConfirmeView.setAdapter(basebean.getStateList());
//                Log.d(TAG, "获取成功");
            }

            @Override
            public void onHandleError(String message) {
                idConfirmeView.hideLoading();
                idConfirmeView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                idConfirmeView.showErr(message);
                idConfirmeView.hideLoading();
                idConfirmeView.nextActivityWithSingleTask(LoginActivity.class);

            }

            @Override
            public void onSubscribe(Disposable d) {
                idConfirmeView.showLoading("获取中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    /**
     * 检查手机号， 获取imei码
     */
    public void checkNum() {

        JSONObject j = new JSONObject();
        j.put("token", idConfirmeView.getToken());
        j.put("phoneNum", idConfirmeView.getNum());

        RetrofitHttpUtil.getInstance().checkPhoneNum(new BaseNoTObserver<ConfirmBean>() {
            @Override
            public void onHandleSuccess(ConfirmBean confirmBean) {

                idConfirmeView.hideLoading();

                //认证成功
                if (confirmBean != null && confirmBean.getData() != null) {

                    Bundle b = new Bundle();
                    b.putString("imei", confirmBean.getData().getImei());
                    b.putString("phoneNum", idConfirmeView.getNum());
                    idConfirmeView.nextActivityWithBundle(SubInfoActivity.class, b);
                }

            }

            @Override
            public void onHandleError(String message) {
                idConfirmeView.hideLoading();
                idConfirmeView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                idConfirmeView.hideLoading();
                idConfirmeView.showErr(message);
                idConfirmeView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                idConfirmeView.showLoading("验证中...");

            }
        }, new RequestBean().setData(j.toString()));

    }

    /**
     * 扫码认证，
     */
    public void checkImei(final String imei) {

        JSONObject j = new JSONObject();
        j.put("token", idConfirmeView.getToken());
        j.put("imei", imei);

        RetrofitHttpUtil.getInstance().checkIMEI(new BaseNoTObserver<ConfirmBean>() {
            @Override
            public void onHandleSuccess(ConfirmBean confirmBean) {
                idConfirmeView.hideLoading();

                //认证成功
                if (confirmBean != null && confirmBean.getData() != null) {
                    Bundle b = new Bundle();
                    b.putString("imei", imei);
                    b.putString("phoneNum", confirmBean.getData().getPhoneNum());
                    idConfirmeView.nextActivityWithBundle(SubInfoActivity.class, b);
                }
            }

            @Override
            public void onHandleError(String message) {
                idConfirmeView.hideLoading();
                idConfirmeView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                idConfirmeView.showErr(message);
                idConfirmeView.hideLoading();
                idConfirmeView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                idConfirmeView.showLoading("验证中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
